// 引入多语言常量
import { LANG, TAGS_VIEW } from '@/constant'
import { getItem, setItem } from '@/utils/storage'

export default {
  namespaced: true,
  state: () => ({
    sidebarOpened: true,
    language: getItem(LANG) || 'zh',
    // 存储tags
    tagsViewList: getItem(TAGS_VIEW) || []
  }),
  mutations: {
    triggerSidebar(state) {
      state.sidebarOpened = !state.sidebarOpened
    },
    // 设置多语言
    setLanguage(state, lang) {
      console.log('set lang')
      state.language = lang
      // 本地化存储
      setItem(LANG, lang)
    },
    // 添加tags
    addTagsViewList(state, tag) {
      // console.log('tag', tag)
      const isFind = state.tagsViewList.find((item) => item.path === tag.path)
      if (!isFind) {
        state.tagsViewList.push(tag)
        setItem(TAGS_VIEW, state.tagsViewList)
      }
    },
    /**
     * @param {type:'other'||'right'||'index', index: index }payload
     */
    // 删除tags
    delTagsViewItem(state, payload) {
      console.log('payload', payload)
      if (payload.type === 'other') {
        state.tagsViewList = state.tagsViewList.filter(
          (item, index) => index === payload.index
        )
      } else if (payload.type === 'right') {
        state.tagsViewList = state.tagsViewList.filter(
          (item, index) => index <= payload.index
        )
      } else if (payload.type === 'index') {
        state.tagsViewList = state.tagsViewList.filter(
          (item, index) => index !== payload.index
        )
      }
      setItem(TAGS_VIEW, state.tagsViewList)
    },
    // 国际化处理title标识
    changeTagsView(state, { index, tag }) {
      state.tagsViewList[index] = tag
      setItem(TAGS_VIEW, state.tagsViewList)
    }
  }
}
